package com.yimi.appstore.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.yimi.appstore.db.AppInfo;
import com.yimi.appstore.db.dao.AppInfoEntityManager;
import com.yimi.appstore.util.Util;

public class ApkReceiver extends BroadcastReceiver {

	private static final String TAG = "ApkReceiver";
	@Override
	public void onReceive(Context context, Intent intent) {
		AppInfoEntityManager appInfoEntityManager = AppInfoEntityManager.getInstance();
		if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){
			String packageName = intent.getDataString().substring(8);
			String info[] = Util.getVersionCode(packageName, context);
			appInfoEntityManager.addApp(new AppInfo(0, info[2], packageName, info[1], 
					info[0], "", 0, -1, -1, null));
		} else if(intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
			String packageName = intent.getDataString().substring(8);
			appInfoEntityManager.deleteApp(packageName);
		}
		
	}

}
